<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permissionList.exportBtn"
          @click="handleExport"
          >导出
        </el-button>
      </template>
      <template slot-scope="{}" slot="accountManagerSearch">
        <select-page
          ref="accountManagerSelect"
          label="accountManagerName"
          value="accountManager"
          :select.sync="search.accountManager"
          :url="website.searchLink.accountManager"
          placeholder-text="客户经理"
          filter-key="searchParam"
          :params="accountManagerParams" 
        ></select-page>
      </template>
      <template slot-scope="{}" slot="orgNoSearch">
        <select-page ref="searchOrgSelect" label="serviceName" value="serviceNo" :select.sync="search.orgNo" :url="website.searchLink.orgUrl" placeholder-text="机构" filter-key="searchParam"></select-page>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page
          ref="searchRpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.rpiNo"
          :url="website.searchLink.merchantUrl"
          placeholder-text="商户"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="serviceNoSearch">
        <select-page
          ref="searchSerSelect"
          label="serviceName"
          value="serviceNo"
          :select.sync="search.serviceNo"
          :url="website.searchLink.serviceUrl"
          placeholder-text="服务商"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{ disabled, size }" slot="beginTimeSearch">
        <el-date-picker
          v-model="search.beginTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="beginTimeChange"
          placeholder="请输入 扣费开始日期"
        ></el-date-picker>
      </template>
      <template slot-scope="{ disabled, size }" slot="endTimeSearch">
        <el-date-picker
          v-model="search.endTime"
          type="datetime"
          default-time="23:59:59"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="endTimeChange"
          placeholder="请输入 扣费结束日期"
        ></el-date-picker>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  getExport
} from "@/api/rb-service/serviceprofit/wlw_fee_charging";
import { mapGetters } from "vuex";
// import { getToken } from "@/util/auth";
// import { downloadFileExcel } from "@/util/util";

export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        // height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        addBtn: false,
        editBtn: false,
        selection: true,
        searchMenuSpan: 6,
        columnBtn: false,
        searchLabelWidth: 110, //搜索框标题宽度
        menu: false,
        column: [
          {
            label: "服务商编号",
            prop: "serviceNo",
            type: "input",
            searchLabel: "服务商",
            search: true,
            searchOrder: 1,
            overHidden: true,
            minWidth:120
          },
          {
            label: "",
            prop: "isSelectChild",
            type: "select",
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            display: false,
            search: true,
            searchType: "checkbox",
            searchSpan: 0,
            searchLabelWidth: "0",
            searchOrder: 1,
            dicData: [
              {
                label: "包含下级",
                value: 1
              }
            ]
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            type: "input",
            overHidden: true,
            minWidth:180
          },
          {
            label: "机构编号",
            searchLabel: "机构",
            prop: "orgNo",
            type: "input",
            overHidden: true,
            search: true,
            searchOrder: 0,
            minWidth:120
          },
          {
            label: "机构名称",
            prop: "orgName",
            type: "input",
            overHidden: true,
            minWidth:180
          },
          {
            label: "用户编号",
            prop: "userNo",
            type: "input",
            overHidden: true,
            minWidth:120
          },
          {
            label: "商户类型",
            prop: "rpiType",
            type: "input",
            type: "select",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=user_income_type",
            search: true,
            overHidden: true,
            minWidth: 80,
            display: false,
            hide:true
          },
          {
            label: "商户类型",
            prop: "rpiTypeName",
            type: "input",
            overHidden: true,
            minWidth: 80,
            display: false,
          },
          {
            label: "商户分类",
            prop: "merchantType",
            type: "select",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=merchant_type",
            search: true,
            overHidden: true,
            minWidth: 80,
            display: false,
            hide:true
          },
          {
            label: "商户分类",
            prop: "merchantTypeName",
            overHidden: true,
            minWidth: 80,
            display: false,
          },
          {
            label: "商户编号",
            prop: "rpiNo",
            type: "input",
            searchLabel: "商户",
            search: true,
            overHidden: true,
            minWidth:140
          },
          {
            label: "商户名称",
            prop: "rpiName",
            type: "input",
            overHidden: true,
            minWidth:180
          },
          {
            label: "商户简称",
            prop: "userShortName",
            type: "input",
            overHidden: true,
            minWidth:180
          },
          {
            label: "商户注册名称",
            prop: "registeredName",
            type: "input",
            overHidden: true,
            minWidth:180
          },
          {
            label: "扣费金额",
            prop: "deductAmount",
            overHidden: true,
            type: "input",
          },
          {
            label: "扣费时间",
            prop: "deductTime",
            type: "input",
            overHidden: true,
            minWidth:140
          },
          {
            label: "扣费次数",
            prop: "deductNum",
            overHidden: true,
            type: "input",
          },
          {
            label: "sn编号",
            prop: "sn",
            type: "input",
            search:true,
            overHidden: true,
            minWidth:140
          },
          // {
          //   label: "通道编号",
          //   prop: "aisleCode",
          //   type: "input",
          //   overHidden: true,
          // },
          {
            label: "通道名称",
            prop: "aisleName",
            type: "input",
            overHidden: true,
          },
          {
            label: "通道名称",
            prop: "aisleKind",
            type: "select",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=aisle_kind",            
            overHidden: true,
            minWidth:80,
            search: true,
            searchOrder: 2,
            display:false,
            hide:true
          },
          {
            label: "客户经理编号",
            prop: "accountManager",
            type: "input",
            searchLabel: "客户经理",
            search: true,
            overHidden: true,
            minWidth:120,
            searchOrder: 1,
          },
          {
            label: "客户经理名称",
            prop: "accountManagerName",
            type: "input",
            overHidden: true,
            minWidth:180
          },
          {
            label: "类型",
            prop: "rptName",
            overHidden: true,
            type: "input",
          },
          {
            label: "供应商",
            prop: "rpsName",
            overHidden: true,
            type: "input",
            minWidth:100
          },
          {
            label: "型号",
            prop: "rpmName",
            overHidden: true,
            type: "input",
          },
          {
            label: "产品类型",
            prop: "productType",
            type: "select",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=product_type",
            search: true,
            overHidden: true,
            minWidth: 80,
            display: false,
            hide:true
          },
          {
            label: "产品类型",
            prop: "productTypeName",
            type: "input",
            display: false,
          },
          {
            label: "扣费开始日期",
            prop: "beginTime",
            type: "input",
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "扣费结束日期",
            prop: "endTime",
            type: "input",
            search: true,
            hide: true,
            display: false,
          },
        ],
      },
      data: [],
      accountManagerParams:{}

    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.wlw_fee_charging_view, false),
        exportBtn: this.vaildData(this.permission.wlw_fee_charging_export, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  watch: {
    "search.serviceNo"(val){
      if(val){
        this.accountManagerParams={serviceNo:val}
        this.search.accountManager="";
        this.$refs.accountManagerSelect.getOptionList(this.accountManagerParams);
      }else{
        this.accountManagerParams={};
        this.search.accountManager="";
        this.$refs.accountManagerSelect.getOptionList({});
      }
    }
  },
  methods: {
    handleExport() {
      let params ={
        ...this.search,
        current:'-1',
        size:'-1'
      }

      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        getExport(params).then((res) => {
            // downloadFileExcel(res);
            this.$message({
              type: "success",
              message: res.data.msg
            });
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    beginTimeChange() {
      if(this.search.endTime){
        if(Date.parse(this.search.beginTime) > Date.parse(this.search.endTime)){
          this.$alert('开始日期不能大于结束日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          .then(() => (this.search.beginTime = ""))
          .catch(() => (this.search.beginTime = ""))
        }
      }
    },
    endTimeChange() {
      if(this.search.beginTime){
        if(Date.parse(this.search.endTime) < Date.parse(this.search.beginTime)){
          this.$alert('结束日期不能小于开始日期', '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
          .then(() => (this.search.endTime = ""))
          .catch(() => (this.search.endTime = ""))
        }
      }
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>

<style>
</style>
